var app=angular.module("orderApp", [])

app.controller("Ordercontroller" ,function($scope,orderService){
	
	//清空模态框
	$('#myModal1').on('hidden.bs.modal', function () {
		$scope.product1=null;
		$scope.username1=null;
		$scope.statue1=null;
		})
	
	

		//得到订单列表
		orderService.getorder().then(
			function successCallback(response) {
				$scope.ls=response.data.rs;
				
		
		}, function errorCallback(response) {
			// 请求失败执行代码
		});
	
		
		//弹出修改订单模态框
	$scope.update=function(o){
		
		$scope.orderid=o.id;
		$scope.product=o.product;
		$scope.username=o.username;
		$scope.statue=o.statue;
		
		
		
		$("#myModal").modal('show');
		
		
		
		
	}
	
	//修改订单
	$scope.submitOrder=function(){
		
		orderService.submitOrder($scope.orderid,$scope.product,$scope.username,$scope.statue).then(
				function successCallback(response) {
					if("Y"==response.data.rs){
						alert("修改成功");
						$("#myModal").modal('hide');
						orderService.getorder().then(
								function successCallback(response) {
									$scope.ls=response.data.rs;
									
							
							}, function errorCallback(response) {
								// 请求失败执行代码
							});
						
					}else{
						alert("修改失败");
						$("#myModal").modal('hide');
					}
					
			
			}, function errorCallback(response) {
				// 请求失败执行代码
			});
		
		
		
	}
	
	//弹出添加订单模态框
	$scope.addOrder=function(){
		$("#myModal1").modal('show');
	}
	
	
	//添加订单
	$scope.addOrder1=function(){
		var pro=$scope.product1;
		var uname=$scope.username1;
		var sta=$scope.statue1;
		if(null==pro){
			alert("产品名不能为空");
		}else if(null==uname){
			alert("客户名不能为空");
		}else if(null==sta){
			alert("订单状态不能为空");
		}else{
			
			orderService.addOrder($scope.product1,$scope.username1,$scope.statue1).then(
					function successCallback(response) {
						if("Y"==response.data.rs){
							alert("添加成功");
							$("#myModal1").modal('hide');
							$scope.product1=null;
							$scope.username1=null;
							$scope.statue1=null;
							orderService.getorder().then(
									function successCallback(response) {
										$scope.ls=response.data.rs;
										
								
								}, function errorCallback(response) {
									// 请求失败执行代码
								});
							
						}else{
							alert("添加失败");
						}
						
				
				}, function errorCallback(response) {
					// 请求失败执行代码
				});
			
		}
		
		
		
		
		
	}
	
	//删除订单
	$scope.deleted=function(o){
		
		orderService.deleted(o.id,o.product,o.username,o.statue).then(
				function successCallback(response) {
					if("Y"==response.data.rs){
						alert("删除订单成功");
						orderService.getorder().then(
								function successCallback(response) {
									$scope.ls=response.data.rs;
									
							
							}, function errorCallback(response) {
								// 请求失败执行代码
							});
						
					}else{
						alert("删除订单失败");
					}
			
			}, function errorCallback(response) {
				// 请求失败执行代码
			});
		
		
	}
	
	
	
	
	
	
})